﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace MvcProject.Models
{
    public class ContractInfoModel
    {
        private LongShareDataContext db = new LongShareDataContext();

        // Query Methods
        public IQueryable<ContractInfo> FindAllContractInfos()
        {
            return db.ContractInfo;
        }
        public IQueryable<ContractInfo> FindAllContractInfosByForeignerId(int foreignerId)
        {
            return db.ContractInfo.Where(m => m.ForeignerId == foreignerId);
        }
        /// <summary>
        /// 获取班组合同
        /// </summary>
        /// <param name="foreignerId"></param>
        /// <returns></returns>
        public IQueryable<ContractInfo> FindTeamContractInfosByForeignerId(int foreignerId)
        {
            return db.ContractInfo.Where(m => m.ForeignerId == foreignerId && m.ContractType == 3);
        }
        /// <summary>
        /// 获取班组合同
        /// </summary>
        /// <param name="foreignerId"></param>
        /// <returns></returns>
        public IQueryable<TeamContractBean> FindTeamContractInfos()
        {
            List<ContractInfo> people = new List<ContractInfo>();
            List<Team> pets = new List<Team>();

            var TeamContract = db.ContractInfo.Join
                (db.Team, c => c.ForeignerId, t => t.TeamId, (c, t) => new TeamContractBean
                {
                    TeamName = t.TeamName,
                    ForeignerId = c.ForeignerId,
                    ContractType = c.ContractType,
                    ContractId = c.ContractId,
                    ContractStartDate = c.ContractStartDate,
                    ContractEndDate = c.ContractEndDate,
                    ContractMoney = c.ContractMoney,
                    ContractName = c.ContractName,
                    CreateDate = c.CreateDate,
                    CreateUserName = c.CreateUserName
                }).Where(m => m.ContractType == 3);
            return TeamContract;
        }
        /// <summary>
        /// 获取项目合同
        /// </summary>
        /// <param name="foreignerId"></param>
        /// <returns></returns>
        public IQueryable<ContractInfo> FindProjectContractInfosByForeignerId(int foreignerId)
        {
            return db.ContractInfo.Where(m => m.ForeignerId == foreignerId && m.ContractType == 1);
        }
        /// <summary>
        /// 获取项目合同
        /// </summary>
        /// <param name="foreignerId"></param>
        /// <returns></returns>
        public IQueryable<ContractInfo> FindProjectContractInfos()
        {
            return db.ContractInfo.Where(m => m.ContractType == 1);
        }

        public ContractInfo GetContractInfo(int contractInfoId)
        {
            return db.ContractInfo.SingleOrDefault(d => d.ContractId == contractInfoId);
        }
        //
        // Insert/Delete Methods
        public void Add(ContractInfo dinner)
        {
            db.ContractInfo.InsertOnSubmit(dinner);
        }
        public void Delete(ContractInfo dinner)
        {
            db.ContractInfo.DeleteOnSubmit(dinner);
        }
        public void DeleteByForeignerId(int foreignerId)
        {
            db.ContractInfo.DeleteAllOnSubmit(FindAllContractInfosByForeignerId(foreignerId));
        }
        //
        // Persistence
        public void Save()
        {
            db.SubmitChanges();
        }
    }
}